package com.datastructure.test.printlistfromtailtohead;

import java.util.ArrayList;
import java.util.Arrays;

public class PrintListFromTailToHead {

    public static void main(String[] args) {

    }

    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> result = new ArrayList<>();
        ListNode head = reverseListNode(listNode);
        while (head != null) {
            result.add(head.val);
            head=head.next;
        }
        return result;
    }

    public ListNode reverseListNode(ListNode listNode){
        ListNode res = null;
        ListNode cur = listNode;
        while (cur!=null){
            ListNode temp = cur.next;
            cur.next=res;
            res=cur;
            cur=temp;
        }
        return res;
    }
}
class ListNode {
    int val;
    ListNode next = null;
    ListNode(int val) {
        this.val = val;
    }
}
